import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mahjong/const/CacheConst.dart';
import 'package:mahjong/util/CacheUtil.dart';
import 'package:mahjong/widget/ChoiceCommon.dart';
import 'package:mahjong/entity/rule/MahjongRule.dart';
import 'package:mahjong/entity/meta/MetaRoomEntity.dart';

class WzMahjongCroom extends StatefulWidget {
  final MetaRoomEntity metaRoomEntity;
  WzMahjongCroom(this.metaRoomEntity);
  @override
  _WzMahjongCroomState createState() => _WzMahjongCroomState();
}

class _WzMahjongCroomState extends State<WzMahjongCroom> {
  MahjongRule roomEntity;
  void setRule() {
    widget.metaRoomEntity.roomRule = roomEntity.toString();
    CacheUtil.preferencesInstance
        .setString(CacheConst.createWZMachjong, json.encode(roomEntity));
  }

  void _setUserCount(dynamic userCount) {
    roomEntity.userCount = userCount;
    setRule();
    setState(() {});
  }

  void _setTurns(dynamic turns) {
    roomEntity.turns = turns;
    setRule();
    setState(() {});
  }

  void _setNumberGame(dynamic numberGame) {
    roomEntity.numberGame = numberGame;
    setRule();
    setState(() {});
  }

  void _setRules(bool change, String value, List<String> rules) {
    if (change) {
      rules.add(value);
    } else {
      rules.remove(value);
    }
    setRule();
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    String createWzMachjongCache =
        CacheUtil.preferencesInstance.getString(CacheConst.createWZMachjong);
    if (createWzMachjongCache == null) {
      roomEntity =
          MahjongRule.init(numberGame: 4, turns: 4, userCount: 4, rules: []);
    } else {
      roomEntity = MahjongRule.fromJson(json.decode(createWzMachjongCache));
    }
    setRule();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ChoiceCommon.singleChoice("人数:", [
          ChoiceCommon.buildRadio(2, "2人", roomEntity.userCount, _setUserCount),
          ChoiceCommon.buildRadio(3, "3人", roomEntity.userCount, _setUserCount),
          ChoiceCommon.buildRadio(4, "4人", roomEntity.userCount, _setUserCount),
        ]),
        ChoiceCommon.singleChoice("局数:", [
          ChoiceCommon.buildRadio(
              2, "2局", roomEntity.numberGame, _setNumberGame),
          ChoiceCommon.buildRadio(
              4, "4局", roomEntity.numberGame, _setNumberGame),
          ChoiceCommon.buildRadio(
              8, "8局", roomEntity.numberGame, _setNumberGame),
          ChoiceCommon.buildRadio(
              16, "16局", roomEntity.numberGame, _setNumberGame),
        ]),
        ChoiceCommon.singleChoice("封顶:", [
          ChoiceCommon.buildRadio(3, "3番", roomEntity.turns, _setTurns),
          ChoiceCommon.buildRadio(4, "4番", roomEntity.turns, _setTurns),
          ChoiceCommon.buildRadio(5, "5番", roomEntity.turns, _setTurns),
          ChoiceCommon.buildRadio(null, "不封顶", roomEntity.turns, _setTurns),
        ]),
        ChoiceCommon.multipleChoice("玩法:", [
          ChoiceCommon.buildMultipleRadio(
              "SING_UP", "报叫", roomEntity.rules, _setRules),
          /* ChoiceCommon.buildMultipleRadio(
              "SHOPING_CARD", "买牌", roomEntity.rules, _setRules), */
        ])
      ],
    );
  }
}
